package com.dd.admin.controller.file;

import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dd.common.convert.SysFileFolderConvert;
import com.dd.common.dto.system.file.folder.SysFileFolderDTO;
import com.dd.common.dto.system.file.folder.SysFileFolderQueryDTO;
import com.dd.common.entity.SysFileFolder;
import com.dd.common.pojo.dto.group.CreateGroup;
import com.dd.common.pojo.dto.group.UpdateGroup;
import com.dd.common.service.SysFileFolderService;
import com.dd.common.utils.AjaxResult;
import com.dd.common.utils.ListUtils;
import com.dd.common.validator.annotation.IDMust;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@SaIgnore
@Validated
@Slf4j
@Tag(name = "文件夹管理", description = "文件夹管理接口")
@RestController
@RequestMapping("/file/folder")
public class FileFolderController {
    @Resource
    private SysFileFolderService sysFileFolderService;

    @Resource
    private SysFileFolderConvert sysFileFolderConvert;

    @Operation(summary = "文件夹新增", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
            required = true,
            content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = SysFileFolderDTO.class))
    ))
    @PostMapping("/add")
    public AjaxResult<Boolean> add(@Validated(CreateGroup.class) @RequestBody SysFileFolderDTO dto) {
        return AjaxResult.success(sysFileFolderService.save(sysFileFolderConvert.dtoToEntity(dto)));
    }

    @Operation(summary = "文件夹编辑", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
            content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = SysFileFolderDTO.class))
    ))
    @PatchMapping("/edit")
    public AjaxResult<Boolean> edit(@Validated(UpdateGroup.class) @RequestBody SysFileFolderDTO dto) {
        return AjaxResult.success(sysFileFolderService.update(new LambdaUpdateWrapper<SysFileFolder>()
                .eq(SysFileFolder::getId, dto.getId())
                .set(dto.getFolderName() != null, SysFileFolder::getFolderName, dto.getFolderName())
                .set(dto.getFolderKey() != null, SysFileFolder::getFolderKey, dto.getFolderKey())
                .set(dto.getParentId() != null, SysFileFolder::getParentId, dto.getParentId())
                .set(dto.getSort() != null, SysFileFolder::getSort, dto.getSort())
                .set(dto.getStatus() != null, SysFileFolder::getStatus, dto.getStatus())
        ));
    }

    @Operation(summary = "文件夹详情", parameters = @Parameter(name = "id", in = ParameterIn.PATH, description = "文件夹ID"))
    @GetMapping("/detail/{id}")
    public AjaxResult<SysFileFolder> detail(@PathVariable @IDMust Long id) {
        return AjaxResult.success(sysFileFolderService.getById(id));
    }

    @Operation(summary = "文件夹删除", parameters = @Parameter(name = "id", in = ParameterIn.PATH, description = "文件夹ID"))
    @DeleteMapping("/delete/{id}")
    public AjaxResult<Boolean> delete(@PathVariable @IDMust Long id) {
        return AjaxResult.success(sysFileFolderService.removeById(id));
    }

    @Operation(summary = "文件夹列表")
    @GetMapping("/list")
    public AjaxResult<Object> list(@Validated SysFileFolderQueryDTO dto) {
        return AjaxResult.success(sysFileFolderService.list(new LambdaQueryWrapper<SysFileFolder>()
                .like(StringUtils.hasText(dto.getFolderName()), SysFileFolder::getFolderName, dto.getFolderName())
                .like(StringUtils.hasText(dto.getFolderKey()), SysFileFolder::getFolderKey, dto.getFolderKey())
                .eq(dto.getParentId() != null, SysFileFolder::getParentId, dto.getParentId())
                .eq(dto.getStatus() != null, SysFileFolder::getStatus, dto.getStatus())
                .ge(dto.getCreatedAtStart() != null, SysFileFolder::getCreateTime, dto.getCreatedAtStart())
                .le(dto.getCreatedAtEnd() != null, SysFileFolder::getCreateTime, dto.getCreatedAtEnd())
                .ge(dto.getUpdatedAtStart() != null, SysFileFolder::getUpdateTime, dto.getUpdatedAtStart())
                .le(dto.getUpdatedAtEnd() != null, SysFileFolder::getUpdateTime, dto.getUpdatedAtEnd())
                .orderByAsc(SysFileFolder::getSort)
        ));
    }

    @Operation(summary = "文件夹树形")
    @GetMapping("/tree")
    public AjaxResult<Object> tree(@Validated SysFileFolderQueryDTO dto) {
        List<SysFileFolder> list = sysFileFolderService.list(new LambdaQueryWrapper<SysFileFolder>()
                .like(StringUtils.hasText(dto.getFolderName()), SysFileFolder::getFolderName, dto.getFolderName())
                .like(StringUtils.hasText(dto.getFolderKey()), SysFileFolder::getFolderKey, dto.getFolderKey())
                .eq(dto.getParentId() != null, SysFileFolder::getParentId, dto.getParentId())
                .eq(dto.getStatus() != null, SysFileFolder::getStatus, dto.getStatus())
                .ge(dto.getCreatedAtStart() != null, SysFileFolder::getCreateTime, dto.getCreatedAtStart())
                .le(dto.getCreatedAtEnd() != null, SysFileFolder::getCreateTime, dto.getCreatedAtEnd())
                .ge(dto.getUpdatedAtStart() != null, SysFileFolder::getUpdateTime, dto.getUpdatedAtStart())
                .le(dto.getUpdatedAtEnd() != null, SysFileFolder::getUpdateTime, dto.getUpdatedAtEnd())
                .orderByAsc(SysFileFolder::getSort));

        return AjaxResult.success(ListUtils.listToTree(JSON.parseArray(JSON.toJSONString(list)), "id", "parentId", "children"));
    }

}
